home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap21 / StrProg / StrProg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.7 KB  |  191 lines

  1. /*--------------------------------------------------------
  2.    STRPROG.C -- Program using STRLIB dynamic-link library
  3.                 (c) Charles Petzold, 1998
  4.   --------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "strlib.h"
  8. #include "resource.h"
  9.  
  10. typedef struct
  11. {
  12.      HDC hdc ;
  13.      int xText ;
  14.      int yText ;
  15.      int xStart ;
  16.      int yStart ;
  17.      int xIncr ;
  18.      int yIncr ;
  19.      int xMax ;
  20.      int yMax ;
  21. }
  22. CBPARAM ;
  23.  
  24. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  25.  
  26. TCHAR szAppName [] = TEXT ("StrProg") ;
  27. TCHAR szString [MAX_LENGTH + 1] ;
  28.  
  29. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                     PSTR szCmdLine, int iCmdShow)
  31. {
  32.      HWND     hwnd ;
  33.      MSG      msg ;
  34.      WNDCLASS wndclass ;
  35.      
  36.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  37.      wndclass.lpfnWndProc   = WndProc ;
  38.      wndclass.cbClsExtra    = 0 ;
  39.      wndclass.cbWndExtra    = 0 ;
  40.      wndclass.hInstance     = hInstance ;
  41.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  42.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  43.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  44.      wndclass.lpszMenuName  = szAppName ;
  45.      wndclass.lpszClassName = szAppName ;
  46.      
  47.      if (!RegisterClass (&wndclass))
  48.      {
  49.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  50.                       szAppName, MB_ICONERROR) ;
  51.           return 0 ;
  52.      }
  53.  
  54.      hwnd = CreateWindow (szAppName, TEXT ("DLL Demonstration Program"),
  55.                           WS_OVERLAPPEDWINDOW,
  56.                           CW_USEDEFAULT, CW_USEDEFAULT,
  57.                           CW_USEDEFAULT, CW_USEDEFAULT,
  58.                           NULL, NULL, hInstance, NULL) ;
  59.      
  60.      ShowWindow (hwnd, iCmdShow) ;
  61.      UpdateWindow (hwnd) ;
  62.      
  63.      while (GetMessage (&msg, NULL, 0, 0))
  64.      {
  65.           TranslateMessage (&msg) ;
  66.           DispatchMessage (&msg) ;
  67.      }
  68.      return msg.wParam ;
  69. }
  70.  
  71. BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  72. {
  73.      switch (message)
  74.      {
  75.      case WM_INITDIALOG:
  76.           SendDlgItemMessage (hDlg, IDC_STRING, EM_LIMITTEXT, MAX_LENGTH, 0) ;
  77.           return TRUE ;
  78.           
  79.      case WM_COMMAND:
  80.           switch (wParam)
  81.           {
  82.           case IDOK:
  83.                GetDlgItemText (hDlg, IDC_STRING, szString, MAX_LENGTH) ;
  84.                EndDialog (hDlg, TRUE) ;
  85.                return TRUE ;
  86.                
  87.           case IDCANCEL:
  88.                EndDialog (hDlg, FALSE) ;
  89.                return TRUE ;
  90.           }
  91.      }
  92.      return FALSE ;
  93. }
  94.  
  95. BOOL CALLBACK GetStrCallBack (PTSTR pString, CBPARAM * pcbp)
  96. {
  97.      TextOut (pcbp->hdc, pcbp->xText, pcbp->yText,
  98.               pString, lstrlen (pString)) ;
  99.      
  100.      if ((pcbp->yText += pcbp->yIncr) > pcbp->yMax)
  101.      {
  102.           pcbp->yText = pcbp->yStart ;
  103.           if ((pcbp->xText += pcbp->xIncr) > pcbp->xMax)
  104.                return FALSE ;
  105.      }
  106.      return TRUE ;
  107. }
  108.  
  109. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  110. {
  111.      static HINSTANCE  hInst ;
  112.      static int        cxChar, cyChar, cxClient, cyClient ;
  113.      static UINT       iDataChangeMsg ;
  114.      CBPARAM           cbparam ;
  115.      HDC               hdc ;
  116.      PAINTSTRUCT       ps ;
  117.      TEXTMETRIC        tm ;
  118.      
  119.      switch (message)
  120.      {
  121.      case WM_CREATE:
  122.           hInst = ((LPCREATESTRUCT) lParam)->hInstance ;
  123.           hdc   = GetDC (hwnd) ;
  124.           GetTextMetrics (hdc, &tm) ;
  125.           cxChar = (int) tm.tmAveCharWidth ;
  126.           cyChar = (int) (tm.tmHeight + tm.tmExternalLeading) ;
  127.           ReleaseDC (hwnd, hdc) ;
  128.  
  129.                // Register message for notifying instances of data changes
  130.  
  131.           iDataChangeMsg = RegisterWindowMessage (TEXT ("StrProgDataChange")) ;
  132.           return 0 ;
  133.           
  134.      case WM_COMMAND:
  135.           switch (wParam)
  136.           {
  137.           case IDM_ENTER:
  138.                if (DialogBox (hInst, TEXT ("EnterDlg"), hwnd, &DlgProc))
  139.                {
  140.                     if (AddString (szString))
  141.                          PostMessage (HWND_BROADCAST, iDataChangeMsg, 0, 0) ;
  142.                     else
  143.                          MessageBeep (0) ;
  144.                }
  145.                break ;
  146.                
  147.           case IDM_DELETE:
  148.                if (DialogBox (hInst, TEXT ("DeleteDlg"), hwnd, &DlgProc))
  149.                {
  150.                     if (DeleteString (szString))
  151.                          PostMessage (HWND_BROADCAST, iDataChangeMsg, 0, 0) ;
  152.                     else
  153.                          MessageBeep (0) ;
  154.                }
  155.                break ;
  156.           }
  157.           return 0 ;
  158.           
  159.      case WM_SIZE:
  160.           cxClient = (int) LOWORD (lParam) ;
  161.           cyClient = (int) HIWORD (lParam) ;
  162.           return 0 ;
  163.                
  164.      case WM_PAINT:
  165.           hdc = BeginPaint (hwnd, &ps) ;
  166.                
  167.           cbparam.hdc   = hdc ;
  168.           cbparam.xText = cbparam.xStart = cxChar ;
  169.           cbparam.yText = cbparam.yStart = cyChar ;
  170.           cbparam.xIncr = cxChar * MAX_LENGTH ;
  171.           cbparam.yIncr = cyChar ;
  172.           cbparam.xMax  = cbparam.xIncr * (1 + cxClient / cbparam.xIncr) ;
  173.           cbparam.yMax  = cyChar * (cyClient / cyChar - 1) ;
  174.                
  175.           GetStrings ((GETSTRCB) GetStrCallBack, (PVOID) &cbparam) ;
  176.               
  177.           EndPaint (hwnd, &ps) ;
  178.           return 0 ;
  179.                
  180.      case WM_DESTROY:
  181.           PostQuitMessage (0) ;
  182.           return 0 ;
  183.  
  184.      default:
  185.           if (message == iDataChangeMsg)
  186.                InvalidateRect (hwnd, NULL, TRUE) ;
  187.           break ;
  188.      }
  189.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  190. }
  191.